package com.demo.demo.controller;

import com.com.base.bean.ResultMessage;
import com.com.base.util.ResultMessageUtil;
import com.demo.demo.domain.UserInfo;
import com.demo.demo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user_info")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/get_account")
    public ResultMessage getAccount(@RequestParam String code) {
        UserInfo userInfo = userInfoService.findByCode(code);
        if (userInfo == null) {
            return ResultMessageUtil.error("此用户不存在");
        } else {
            return ResultMessageUtil.success("",userInfo);
        }
    }

    @RequestMapping("/login")
    public ResultMessage login(@RequestBody UserInfo user_info) {

        UserInfo userInfo = userInfoService.findByCodeAndAccount(user_info.getCode(), user_info.getAccount());
        if (userInfo == null) {
            return ResultMessageUtil.error("此用户不存在");
        } else {
            if (userInfo.getPassword().equals(user_info.getPassword())) {
                return ResultMessageUtil.success("登陆成功", userInfo);
            } else {
                return ResultMessageUtil.error("密码错误");
            }
        }
    }
}
